package com.example.yyy

import android.content.Context
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import android.widget.BaseAdapter
import android.widget.ImageView
import android.widget.TextView

class MyListAdapter(private val context: Context,private val listArray: MutableList<Planet>) : BaseAdapter() {
    override fun getView(position: Int, convertView: View?, parent: ViewGroup?): View {
        var  view: View? = convertView
        val holder: ViewHolder
        val planet = listArray[position]
        if (convertView==null) {
            view = LayoutInflater.from(context).inflate(R.layout.list_layout,null)
            holder = ViewHolder()
            holder.im = view.findViewById(R.id.iv_lt_planet)
            holder.tv = view.findViewById(R.id.tv_lt_planet)
            view.tag = holder
        }else{
            holder= view?.tag as ViewHolder
        }
        holder.im.setImageResource(planet.image)
        holder.tv.text = planet.str
        return view!!
    }

    private class ViewHolder {
        lateinit var im: ImageView
        lateinit var tv: TextView
    }

    override fun getItem(position: Int): Any = listArray[position]

    override fun getItemId(position: Int): Long = position.toLong()

    override fun getCount(): Int = listArray.size
}